%% a script to make wwcom background 2014

red= reshape([192 32 0]./255,[1 1 3]);
white= reshape([1 1 1],[1 1 3]);

imWidth= 550;
lineTheta= -20./180 .* pi;
m= tan(lineTheta);
imHeight= -ceil(m*imWidth);
im= ones(imHeight,imWidth,3);


thickRedLineY= 0:imHeight:imHeight*2;
thickRedLineWidth= repmat(40,[1 length(thickRedLineY)]);
thickRedLineSigma= repmat(1,[1 length(thickRedLineY)]);
thickRedLineColour= repmat(red,[length(thickRedLineY) 1]);


thinWhiteLineY= 0:imHeight/9:imHeight*2;
thinWhiteLineWidth= repmat(1,[1 length(thinWhiteLineY)]);
thinWhiteLineSigma= repmat(0.5,[1 length(thinWhiteLineY)]);
thinWhiteLineColour= repmat(white,[length(thinWhiteLineY) 1]);

thinRedLineY= 0:imHeight/9:imHeight*4;
thinRedLineY([1:3 8:12 17:19])= [];
thinRedLineWidth= repmat(0.25,[1 length(thinRedLineY)]);
thinRedLineSigma= repmat(0.5,[1 length(thinRedLineY)]);
thinRedLineColour= repmat(red,[length(thinRedLineY) 1]);

lineWidths= [thickRedLineWidth thinWhiteLineWidth thinRedLineWidth];
lineSigmas= [thickRedLineSigma thinWhiteLineSigma thinRedLineSigma];
lineYs= [thickRedLineY thinWhiteLineY thinRedLineY];
lineColours= cat(1,thickRedLineColour,thinWhiteLineColour,thinRedLineColour);


OptCPPLoadLibrary('GeometryMatlab');
[gy,gx]= meshgrid(1:imHeight,1:imWidth);

for l_i=1:length(lineWidths)
    lineWidth= lineWidths(l_i);
    lineSigma= lineSigmas(l_i);
    
    lineY= lineYs(l_i);
    lineSegX= [-imWidth 2*imWidth];
    lineSegY= lineY + m.*lineSegX;
    
    lineSeg= [lineSegX.' lineSegY.'];
    
    points= [gx(:) gy(:)];
    [projPoints] = OptGeoProject2DPointsOntoPolyline(points, lineSeg);
    
    d= sqrt(sum((points-projPoints).^2,2));
    
    vInd= d<(lineWidth + 3*lineSigma);
    pv= d-lineWidth;
    pv(pv<0)= 0;
    
    pv= exp(-((pv.^2)./(2*lineSigma.^2)));
    
    points= points(vInd,:);
    pv= pv(vInd);
    
    ind= sub2ind([imHeight imWidth],round(points(:,2)),round(points(:,1)));
    
    alphaIm= zeros(imHeight,imWidth);
    alphaIm(ind)= pv;
    
    im= bsxfun(@times,lineColours(l_i,:,:),alphaIm) + bsxfun(@times,im,(1-alphaIm));
    
end

figure(1); clf; imagesc(im);
% hold on; plot(lineX,lineY);
imwrite(im,'C:\html\wwcom\html\images\backgrounds\2014home.png');


%% away image

purple= reshape([116 48 190]./255,[1 1 3]);
white= reshape([1 1 1],[1 1 3]);

imWidth= 5;
lineTheta= 0;
m= tan(lineTheta);
imHeight= 100;
im= ones(imHeight,imWidth,3);


thickPurpleLineY= imHeight./2;
thickPurpleLineWidth= imHeight;
thickPurpleLineSigma= 1;
thickPurpleLineColour= purple;


thinWhiteLineY= imHeight/2;
thinWhiteLineWidth= 5;
thinWhiteLineSigma= 0.5;
thinWhiteLineColour= white;

lineWidths= [thickPurpleLineWidth thinWhiteLineWidth];
lineSigmas= [thickPurpleLineSigma thinWhiteLineSigma];
lineYs= [thickPurpleLineY thinWhiteLineY];
lineColours= cat(1,thickPurpleLineColour,thinWhiteLineColour);


OptCPPLoadLibrary('GeometryMatlab');
[gy,gx]= meshgrid(1:imHeight,1:imWidth);

for l_i=1:length(lineWidths)
    lineWidth= lineWidths(l_i);
    lineSigma= lineSigmas(l_i);
    
    lineY= lineYs(l_i);
    lineSegX= [-imWidth 2*imWidth];
    lineSegY= lineY + m.*lineSegX;
    
    lineSeg= [lineSegX.' lineSegY.'];
    
    points= [gx(:) gy(:)];
    [projPoints] = OptGeoProject2DPointsOntoPolyline(points, lineSeg);
    
    d= sqrt(sum((points-projPoints).^2,2));
    
    vInd= d<(lineWidth + 3*lineSigma);
    pv= d-lineWidth;
    pv(pv<0)= 0;
    
    pv= exp(-((pv.^2)./(2*lineSigma.^2)));
    
    points= points(vInd,:);
    pv= pv(vInd);
    
    ind= sub2ind([imHeight imWidth],round(points(:,2)),round(points(:,1)));
    
    alphaIm= zeros(imHeight,imWidth);
    alphaIm(ind)= pv;
    
    im= bsxfun(@times,lineColours(l_i,:,:),alphaIm) + bsxfun(@times,im,(1-alphaIm));
    
end

figure(1); clf; imagesc(im);
% hold on; plot(lineX,lineY);
imwrite(im,'C:\html\wwcom\html\images\backgrounds\2014away.png');

%% colour shift logo

[c,map]= imread('C:\html\wwcom\html\images\2009Banner_Home.gif');

im= zeros(size(c,1),size(c,2),3);
for i=1:size(c,1)
    for j=1:size(c,2)
        im(i,j,:)= map(c(i,j)+1,:);
    end
end

base= reshape([0.8824    0.2941    0.2686],[1 1 3]);
him= rgb2hsv(im);
d= rgb2hsv(base) - rgb2hsv(purple);

him(:,:,1)= him(:,:,1) - d(1);
vInd= him(:,:,3)<0.95;
for i=2:3
    t= him(:,:,i);
    t(vInd)= t(vInd) - d(i);
    him(:,:,i)= t;
end
% him(:,:,2)= him(:,:,2) - d(2);

im= hsv2rgb(him);
im= uint8(im.*255);
% im= uint8(bsxfun(@minus,double(im)./255,d).*255);

imwrite(im,'C:\html\wwcom\html\images\2014Banner_Away.png');

%% colour shift menu
[im]= imread('C:\html\wwcom\html\images\mnuSelect.jpg');

base= reshape([185 20 22]./255,[1 1 3]);
% him= rgb2hsv(double(im)./255);
im= double(im)./255;
d= base - purple;

for i=1:3
    t= im(:,:,i);
    t= t - d(i);
    im(:,:,i)= t;
end
% him(:,:,1)= him(:,:,1) - d(1);
% him(:,:,2)= him(:,:,2) - d(2);

% im= hsv2rgb(him);
im= uint8(im.*255);
% im= uint8(bsxfun(@minus,double(im)./255,d).*255);

imwrite(im,'C:\html\wwcom\html\images\mnuSelect2014away.png');

%% colour shift theme

rootDir= 'C:\html\wwcom\html\styles\home\imageset';
rootDir= 'C:\html\wwcom\html\styles\home\imageset\en';
rootDir= 'C:\html\wwcom\html\styles\home\theme\images';
outputDir= 'C:\html\wwcom\html\styles\2014away\imageset';
outputDir= 'C:\html\wwcom\html\styles\2014away\imageset\en';
outputDir= 'C:\html\wwcom\html\styles\2014away\theme\images';

fList= dir(fullfile(rootDir,'*.gif'));

for f_i=1:length(fList)
    [c,map]= imread(fullfile(rootDir,fList(f_i).name));        
        
    base= reshape([0.8824    0.2941    0.2686],[1 1 3]);
    him= rgb2hsv(map);
    d= rgb2hsv(base) - rgb2hsv(purple);
    
    him(:,1)= him(:,1) - d(1);
    him(him<0)= him(him<0)+1;
    him(him>1)= him(him>1)-1;
    
    im= hsv2rgb(him);
%     im= uint8(im.*255);
    
    [info]= imfinfo(fullfile(rootDir,fList(f_i).name));        
    if isfield(info,'TransparentColor')
        imwrite(c,im,fullfile(outputDir,fList(f_i).name),'gif','TransparentColor',info(1).TransparentColor-1);
    else
        imwrite(c,im,fullfile(outputDir,fList(f_i).name));
    end        
end

